
import cn.fcj.f20240328.fsocket.client.FNIOSocketClient;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println("==hextodouble====:"+parser("0000b442"));
        FNIOSocketClient server=new FNIOSocketClient();
        server.setPort(8888)
                .setIp("192.168.11.41")
                .setRuning(true)
                .setSocketEventHandler(new SocketFixedLengthHandlerImp())
                .startSocket();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Scanner scanner = new Scanner(System.in);
                System.out.println("请输入您要发送给服务端的消息");
                System.out.println("=========================================================");
                while (scanner.hasNextLine()) {
                    String s = scanner.nextLine();
                    if(s==null){
                        continue;
                    }
                    //大家佛挡杀佛撒旦发生的发家撒旦发生的发多少是的发的说法的说法撒旦发生的发东山风动石水电费水电费撒旦发生的发水电费到沙发上地方多少时代发生的发生地方水电分离科技手段发时代领峰拒收到付时代峰峻少得可怜发时代峰峻乐山大佛是
//a1e095984ab1a2e095984ab2a3e095984ab3a40000b442b4a500700f46b5a600000000b6a700000000b7a800000000b8a9ffffffffb9aa00004040ba
                    //a100d0e346b1
                    // a20000b442b2
                    // a300ec6547b3
                    // a4e095984ab4
                    // a580b75648b5
                    // a600000000b6
                    // a7ffffffffb7
                    // aa00004040ba
                    /**
                     * A: 29160.0
                     * B: 90.0
                     * C: 58860.0
                     * D: 4999920.0
                     * E: 219870.0
                     * Temp: NaN
                     * num: 3.0
                     * a100d0e346b1a20000b442b2a300ec6547b3a4e095984ab4a580b75648b5a600000000b6a700000000b7a800000000b8a900000000b9aa00004040ba
                     * a100d0e346b1a20000b442b2a300ec6547b3a4e095984ab4a580b75648b5a600000000b6a700000000b7a800000000b8a9ffffffffb9aa00004040ba
                     */
                    ByteBuffer buffer = ByteBuffer.allocate(60);
                    float datas[] = new float[]{29160.0f, 90.0f, 58860.0f, 4999920.0f, 219870.0f, 0f,0f,0f,0f};
                    ByteBuffer floatBuff=ByteBuffer.allocate(4);

                    for (int i=0;i<datas.length;i++) {
                        buffer.put((byte) (161+i));
                        floatBuff.order(ByteOrder.LITTLE_ENDIAN);
                        floatBuff.putFloat(datas[i]);
                        buffer.put(floatBuff.array());
                        buffer.put((byte) (177+i));
                        floatBuff.clear();
                    }
                    buffer.put((byte) 170);
                    floatBuff.order(ByteOrder.LITTLE_ENDIAN);
                    floatBuff.putFloat(3.0f);
                    buffer.put(floatBuff.array());
                    floatBuff.clear();
                    buffer.put((byte) 186);
                    server.sendDataForChannel(null,buffer.array());

                }
            }
        }).start();
    }
    public static double parser(String hexString) {
        int hexStringLength = hexString.length();
        if (hexStringLength % 2 != 0) {
            System.out.println("无效的16进制字符串，长度应为偶数：" + hexString);
            return -1;
        }

// 倒序排列16进制字节
        StringBuilder reversedHexString = new StringBuilder();
        for (int i = hexStringLength - 2; i >= 0; i -= 2) {
            reversedHexString.append(hexString.substring(i, i + 2));
        }

// 将16进制字符串解释为IEEE 754单精度浮点数
        int intValue =  (int)Long.parseLong(reversedHexString.toString(), 16);
        System.out.println("==hextodouble==intValue==:"+intValue);
        float floatValue = Float.intBitsToFloat(intValue);
//        double floatValue = Double.longBitsToDouble(intValue);
// 输出浮点数值
//        System.out.println("浮点数值: " + floatValue);
        return floatValue;
    }

}